/*
 * Copyright 2009 Cedric Priscal
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */

package android_serialport_api.sample;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.clouddot.baipai.R;
import com.clouddot.baipai.infrastructure.environment.Env;

import android_serialport_api.SerialPort;

public class MainMenu extends Activity {

    private SharedPreferences spf;
    private final String SP_NAME = "sp";

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_t_main);

        spf = getSharedPreferences(SP_NAME, 0);
        ((application) getApplication()).setBaudrate(spf.getString("BAUDRATE", ""));
        ((application) getApplication()).setDevice(spf.getString("DEVICE", ""));

        try {
//            /dev/ttyS1
//            9600
            String newValue = "/dev/ttyS1";
            String path = "9600";
//            preference.setSummary((String)newValue);
//            preference.setSummary((String)path);

            ((application) getApplication()).setDevice((String)newValue);

            SharedPreferences sp = getSharedPreferences("android_serialport_api.sample_preferences", MODE_PRIVATE);
//            String path = sp.getString("DEVICE", "");
//            int baudrate = Integer.decode(sp.getString("BAUDRATE", "-1"));
//            Log.e("serialPortPreferences", "path:" + path + ", baudrate:" + baudrate + ", pathNewValue:" + newValue);

            SharedPreferences spf = getSharedPreferences("sp", 0);
            spf.edit().putString("DEVICE", (String)newValue).commit();

            ((application) getApplication()).setBaudrate((String)path);

//            SharedPreferences sp = getSharedPreferences("android_serialport_api.sample_preferences", MODE_PRIVATE);
//            String path = sp.getString("DEVICE", "");
//            int baudrate = Integer.decode(sp.getString("BAUDRATE", "-1"));
//            Log.e("serialPortPreferences", "path:" + path + ", baudrate:" + baudrate + ", pathNewValue:" + newValue);

//            SharedPreferences spf = getSharedPreferences("sp", 0);
            spf.edit().putString("BAUDRATE", (String)newValue).commit();

        } catch (Throwable e) {
            e.printStackTrace();
        }

        final Button buttonSetup = (Button) findViewById(R.id.ButtonSetup);
        buttonSetup.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(MainMenu.this, SerialPortPreferences.class));
            }
        });

        final Button buttonConsole = (Button) findViewById(R.id.ButtonConsole);
        buttonConsole.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(MainMenu.this, ConsoleActivity.class));
            }
        });

        final Button buttonLoopback = (Button) findViewById(R.id.ButtonLoopback);
        buttonLoopback.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(MainMenu.this, LoopbackActivity.class));
            }
        });

        final Button button01010101 = (Button) findViewById(R.id.Button01010101);
        button01010101.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(MainMenu.this, Sending01010101Activity.class));
            }
        });

        final Button buttonAbout = (Button) findViewById(R.id.ButtonAbout);
        buttonAbout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainMenu.this);
                builder.setTitle("About");
                builder.setMessage(R.string.about_msg);
                builder.show();
            }
        });

        final Button buttonQuit = (Button) findViewById(R.id.ButtonQuit);
        buttonQuit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                MainMenu.this.finish();
            }
        });

        final Button cs = (Button) findViewById(R.id.cs);
        cs.setVisibility(View.GONE);
        cs.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                try {
                    SerialPort mPort = ((application) getApplication()).getSerialPort(Env.getAppContext().DevicePath);
                    InputStream is = mPort.getInputStream();
                    Log.e("aaaaaaaaa", "aaaaaaaaaaaa" + convertStreamToString(is));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "/n");
                System.out.println(sb);
            }
            System.out.println("读取完成");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}
